package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SysNoticeDTO;
import com.andy.blog.admin.service.SysNoticeBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.vo.SysNoticeVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公告
 * Created by andy on 2023-04-05 11:38
 */
@RestController
//@RequestMapping("/system/notice")
public class SysNoticeController {

    @Resource
    private SysNoticeBusService sysNoticeBusService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/system/notice/list")
    public GeneralResponseResult<PageUtilsDTO<SysNoticeDTO>> list(SysNoticeVO sysConfigVO) {
        return GeneralResponseResult.success(sysNoticeBusService.selectListByPage(sysConfigVO));
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/system/notice/{id}")
    public GeneralResponseResult<SysNoticeDTO> getInfo(@PathVariable Long id) {
        return GeneralResponseResult.success(sysNoticeBusService.selectById(id));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/system/notice")
    public GeneralResponseResult<Void> add(@Validated @RequestBody SysNoticeVO sysNoticeVO) {
        sysNoticeBusService.insert(sysNoticeVO);
        return GeneralResponseResult.success();
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/system/notice")
    public GeneralResponseResult<Void> edit(@Validated @RequestBody SysNoticeVO sysNoticeVO) {
        sysNoticeBusService.updateById(sysNoticeVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/system/notice/{ids}")
    public GeneralResponseResult<Void> remove(@PathVariable List<Long> ids) {
        sysNoticeBusService.deleteByIds(ids);
        return GeneralResponseResult.success();
    }


    /**
     * 门户-公告
     */
    @GetMapping("/portal/notice/talk")
    public GeneralResponseResult<List<String>> talk() {
        return GeneralResponseResult.success(sysNoticeBusService.talk());
    }
}
